package com.heima.controller;

import cn.hutool.core.util.ObjectUtil;
import com.heima.anno.NoAuth;
import com.heima.pojos.User;
import com.heima.service.KsClassService;
import com.heima.service.UserService;
import com.heima.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/v1")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 根据账号判断是否是老师
     *
     * @param mobile
     * @return
     */
    //todo
    @NoAuth
    @GetMapping("/login/{mobile}")
    public R selectTeacher(@PathVariable String mobile) {
        return userService.selectTeacher(mobile);
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @NoAuth
    @PostMapping("/login/in")
    public R login(@RequestBody User user, HttpSession session) {
        user.setMobile(user.getName());
        R loginR = userService.login(user);
        if (ObjectUtil.isNotNull(loginR.getData())) session.setAttribute("user", loginR.getData());
        return loginR;
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("/user/findByClassId")
    public R selectAll() {
        return userService.selectAll();
    }

    /**
     * 修改积分
     *
     * @param user
     * @return
     */
    @PostMapping("/user/addIntegral")
    public R updateintegral(@RequestBody User user) {
        return userService.updateintegral(user);
    }

    /**
     * 重置全班积分
     *
     * @param classId
     * @return
     */
    @GetMapping("/user/resetGScoreByGroupId/{classId}")
    public R re(@PathVariable Long[] classId) {
        return userService.re(classId);
    }

}
